home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / SprocketInvaders / Source / GameObject.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  5.8 KB  |  292 lines  |  [TEXT/MPS ]

  1. //•    ------------------------------------------------------------------------------------------    •
  2. //•
  3. //•    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  4. //•
  5. //•
  6. //•        You may incorporate this sample code into your applications without
  7. //•        restriction, though the sample code has been provided "AS IS" and the
  8. //•        responsibility for its operation is 100% yours.  However, what you are
  9. //•        not permitted to do is to redistribute the source as "DSC Sample Code"
  10. //•        after having made changes. If you're going to re-distribute the source,
  11. //•        we require that you make it clear in the source that the code was
  12. //•        descended from Apple Sample Code, but that you've made changes.
  13. //•
  14. //•        Authors:
  15. //•            Chris De Salvo
  16. //•
  17. //•    ------------------------------------------------------------------------------------------    •
  18.  
  19. //•    ------------------------------    Includes
  20.  
  21. #include "GameObject.h"
  22. #include "Graphics.h"
  23. #include "MemoryHandler.h"
  24. #include "ObjectActions.h"
  25.  
  26. //•    ------------------------------    Private Definitions
  27. //•    ------------------------------    Private Types
  28. //•    ------------------------------    Private Variables
  29.  
  30. static UInt32    gNumGameObjectsEver = 0;
  31.  
  32. //•    ------------------------------    Private Functions
  33. //•    ------------------------------    Public Variables
  34.  
  35. UInt32    gNumGameObjects = 0;
  36.  
  37. //•    --------------------    GameObjectAllocate
  38.  
  39. GameObjectPtr
  40. GameObjectAllocate(void)
  41. {
  42. GameObjectPtr    go;
  43.  
  44.     //•    Allocate memory for this object
  45.     go = (GameObjectPtr) NewTaggedPtrClear(sizeof (GameObject), 'GObj', gNumGameObjectsEver++);
  46.     if (! go)
  47.         return (nil);
  48.  
  49.     gNumGameObjects++;
  50.  
  51.     go->action = ObjectIdle;
  52.     SetRect(&go->screenRect, 0, 0, 0, 0);
  53.     SetRect(&go->oldScreenRect, 0, 0, 0, 0);
  54.  
  55.     return (go);
  56. }
  57.  
  58. //•    --------------------    GameObjectDispose
  59.  
  60. void
  61. GameObjectDispose(GameObjectPtr go)
  62. {
  63.     if (! go)
  64.         return;
  65.  
  66.     switch (go->kind)
  67.     {
  68.         case objectEnemyParticles:
  69.         case objectPlayerParticles:
  70.         case objectPlasmaParticles:
  71.         case objectMissileParticles:
  72.             ParticlesDispose(&go->objectData.particles);
  73.             break;
  74.     }
  75.     
  76.     DisposeTaggedPtrZ((Ptr *) &go);
  77.     gNumGameObjects--;
  78. }
  79.  
  80. //•    --------------------    GameObjectAddToList
  81.  
  82. void
  83. GameObjectAddToList(GameObjectPtr *list, GameObjectPtr go)
  84. {
  85.     if (! go)
  86.         return;
  87.  
  88.     //•    Handle the case of this being the only item
  89.     if (*list == nil)
  90.     {
  91.         *list = go;
  92.         go->next = nil;
  93.         go->prev = nil;
  94.         
  95.         return;
  96.     }
  97.  
  98.     //•    Otherwise, just add it to the top
  99.     go->next = *list;            //•    Have the new object point down to the old front
  100.     go->prev = nil;                //•    Point up to nothing
  101.     go->next->prev = go;        //•    Have the next item point back up to the new object
  102.  
  103.     *list = go;                    //•    Update the global list head pointer
  104. }
  105.  
  106. //•    --------------------    GameObjectRemoveFromList
  107.  
  108. void
  109. GameObjectRemoveFromList(GameObjectPtr *list, GameObjectPtr go)
  110. {
  111.     if (! go)
  112.         return;
  113.  
  114.     if (! *list)
  115.         return;
  116.  
  117.     //•    Adjust the back link
  118.     if (go->prev)
  119.         go->prev->next = go->next;
  120.  
  121.     //•    Adjust the forward link
  122.     if (go->next)
  123.         go->next->prev = go->prev;
  124.  
  125.     //•    Handle the case of it being the first item
  126.     if (go == *list)
  127.     {
  128.         if (go->next)
  129.             *list = go->next;
  130.         else
  131.             *list = nil;
  132.     }
  133. }
  134.  
  135. //•    --------------------    GameObjectDisposeList
  136.  
  137. void
  138. GameObjectDisposeList(GameObjectPtr *list)
  139. {
  140. GameObjectPtr    next;
  141. GameObjectPtr    current;
  142.  
  143.     if (! *list)
  144.         return;
  145.         
  146.     current = *list;
  147.     
  148.     do
  149.     {
  150.         next = current->next;
  151.         GameObjectDispose(current);
  152.         current = next;
  153.     } while (next);
  154.  
  155.     *list = nil;
  156. }
  157.  
  158. //•    --------------------    GameObjectSetSprite
  159.  
  160. void
  161. GameObjectSetSprite(GameObjectPtr go, SpritePtr sprite)
  162. {
  163.     if (go && sprite)
  164.         go->objectData.sprite = sprite;
  165. }
  166.  
  167. //•    --------------------    GameObjectGetSprite
  168.  
  169. SpritePtr
  170. GameObjectGetSprite(GameObjectPtr go)
  171. {
  172.     if (! go)
  173.         return (nil);
  174.         
  175.     return (go->objectData.sprite);
  176. }
  177.  
  178. //•    --------------------    GameObjectSetParticles
  179.  
  180. void
  181. GameObjectSetParticles(GameObjectPtr go, ParticlesPtr particles)
  182. {
  183.     if (go && particles)
  184.     {
  185.         go->objectData.particles = particles;
  186.         go->objectData.particles->bounds = go->bounds;
  187.     }
  188. }
  189.  
  190. //•    --------------------    GameObjectGetParticles
  191.  
  192. ParticlesPtr
  193. GameObjectGetParticles(GameObjectPtr go)
  194. {
  195.     if (! go)
  196.         return (nil);
  197.         
  198.     return (go->objectData.particles);
  199. }
  200.  
  201. //•    --------------------    GameObjectSetBounds
  202.  
  203. void
  204. GameObjectSetBounds(GameObjectPtr go, RectPtr r)
  205. {
  206.     if (go && r)
  207.         go->bounds = *r;
  208. }
  209.  
  210. //•    --------------------    GameObjectGetBounds
  211.  
  212. RectPtr
  213. GameObjectGetBounds(GameObjectPtr go)
  214. {
  215.     if (! go)
  216.         return (nil);
  217.  
  218.     return (&go->bounds);
  219. }
  220.  
  221. //•    --------------------    GameObjectListAdvance
  222.  
  223. void
  224. GameObjectListAdvance(GameObjectPtr list)
  225. {
  226. GameObjectPtr    next;
  227. GameObjectPtr    current;
  228.  
  229.     if (! list)
  230.         return;
  231.  
  232.     current = list;
  233.  
  234.     //•    We need to iterate over the list in this manner because
  235.     //•    objects might remove themselves from the list as part of their action
  236.     do
  237.     {
  238.         next = current->next;
  239.         current->action(current);
  240.         current = next;
  241.     } while (next);
  242. }
  243.  
  244. //•    --------------------    GameObjectListDraw
  245.  
  246. void
  247. GameObjectListDraw(GameObjectPtr list, CGrafPtr dest)
  248. {
  249. GameObjectPtr    index;
  250.  
  251.     for (index = list; index; index = index->next)
  252.     {
  253.         switch (index->kind)
  254.         {
  255.             case objectEnemy:
  256.             case objectEnemyShot:
  257.             case objectGreenPlayer:
  258.             case objectGreenPlayerShot:
  259.             case objectRedPlayer:
  260.             case objectRedPlayerShot:
  261.                 index->screenRect = SpriteDraw(index->objectData.sprite, index->frame, dest, index->screenX, index->screenY);
  262.                 break;
  263.                 
  264.             case objectEnemyParticles:
  265.             case objectPlayerParticles:
  266.             case objectPlasmaParticles:
  267.             case objectMissileParticles:
  268.                 index->screenRect = ParticlesDraw(index->objectData.particles, dest);
  269.                 break;
  270.         }
  271.  
  272.         if (! EmptyRect(&index->screenRect))
  273.             GraphicsSetRectDirty(&index->screenRect);
  274.             
  275.         index->oldScreenRect = index->screenRect;
  276.     }
  277. }
  278.  
  279. //•    --------------------    GameObjectListCount
  280.  
  281. UInt32
  282. GameObjectListCount(GameObjectPtr list)
  283. {
  284. GameObjectPtr    index;
  285. UInt32        count = 0;
  286.  
  287.     for (index = list; index; index = index->next)
  288.         count++;
  289.  
  290.     return (count);
  291. }
  292.